package org.goyo.main.domain.dao.pur.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.goyo.common.exception.DBException;
import org.goyo.common.utils.Pager;
import org.goyo.common.utils.ToolUtils;
import org.goyo.main.domain.dao.BaseDAO;
import org.goyo.main.domain.dao.pur.IPurOrderDao;
import org.goyo.main.domain.entity.pur.PurOrderEntry;
import org.springframework.stereotype.Repository;
@Repository
public class PurOrderDaoImpl extends BaseDAO implements IPurOrderDao{

	@Override
	public Pager findByCond(String cond, Pager pager,Object[] groupUuids) throws DBException {
		StringBuffer buf = new StringBuffer("from PurOrder where 1=1 ");
		buf.append(" and depGroup in ");
		buf.append(ToolUtils.getInParamter(groupUuids));
		if (StringUtils.isNotBlank(cond)) {
		  buf.append(" and ");
          buf.append(cond);
		}
	    String hql = this.addSort(buf, pager);
	    String countHql = "select count(*) "+buf.toString();
		return this.findObjectsByPager(hql,countHql, pager);
	}

	@Override
	public List<PurOrderEntry> findByParentId(String parentId) throws DBException {
		String hql = "from PurOrderEntry where parentId=?";
		return this.findObjects(hql, null, new Object[]{parentId});
	}

	@Override
	public void updateSumPayAmount(BigDecimal amount, String uuid)
			throws DBException {
		String hql = "update PurOrder set sumPayAmount=?  where uuid=?";
		this.updateByHql(hql, new Object[]{amount,uuid});
	}
	

	@Override
	public Pager findByEntryCond(String cond, Pager pager) throws DBException {
		
		StringBuffer buf = new StringBuffer("from PurOrderEntry where 1=1 ");
		if (StringUtils.isNotBlank(cond)) {
		  buf.append(" and ");
          buf.append(cond);
		}
	    String hql = this.addSort(buf, pager);
	    String countHql = "select count(*) "+buf.toString();
		return this.findObjectsByPager(hql,countHql, pager);
	}

}
